package com.alpaak.common.utils;

import com.alpaak.common.enumtype.ErrorCodeEnum;
import com.alpaak.common.exception.BusinessException;

import java.io.*;

public class FileUtil {
	
	/**
	 * @Title: readTextFile  
	 * @Description: 读txt文件，转换为字符串输出
	 * @param @param file        文件绝对路径
	 * @param @param charsetName 字符编码
	 * @param @return
	 * @param @throws BusinessException     
	 * @return String      		   返回值
	 * @throws
	 */
	public static String readTextFile(String file, String charsetName)
			throws BusinessException {
		BufferedReader br = null;
		InputStreamReader in = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(file);
			if (charsetName == null || "".equals(charsetName))
				in = new InputStreamReader(fis, "UTF-8");
			else {
				in = new InputStreamReader(fis, charsetName);
			}
			br = new BufferedReader(in);

			String data = null;
			StringBuffer sb = new StringBuffer();
			while ((data = br.readLine()) != null) {
				sb.append(data);
				sb.append("\n");
			}
			br.close();
			String str1 = sb.toString();
			return str1;
		} catch (FileNotFoundException fe) {
			throw new BusinessException(ErrorCodeEnum.FILE_NOT_FOUND.getErrCode(), ErrorCodeEnum.FILE_NOT_FOUND.getErrMsg());
		} catch (IOException ie) {
			throw new BusinessException(ErrorCodeEnum.FILE_IO.getErrCode(), ErrorCodeEnum.FILE_IO.getErrMsg());
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fis != null){
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void main(String[] args) {
		String xml = readTextFile("F:\\福建省立医院接口\\MDT03.txt","GBK");
		System.out.println(xml);
	}

}
